package com.sdleyou.treepro.common.rabbitmq.receiver;

import com.sdleyou.treepro.common.rabbitmq.AmqpExchange;
import com.sdleyou.treepro.common.rabbitmq.event.StepChangeEvent;
import com.sdleyou.treepro.common.rabbitmq.message.StepChangeMsg;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 商品变化消费者
 * 
 * @author fk
 * @version v2.0
 * @since v7.0.0 2018年3月23日 上午10:29:54
 */
@Component
public class StepChangeReceiver {

	protected final Log logger = LogFactory.getLog(this.getClass());

	@Autowired(required = false)
	private List<StepChangeEvent> events;

	/**
	 * 步数变化
	 * 
	 * @param stepChangeMsg
	 */
	@RabbitListener(bindings = @QueueBinding(
			value = @Queue(value = AmqpExchange.TOTAL_STEP_CHANGE + "_QUEUE"),
			exchange = @Exchange(value = AmqpExchange.TOTAL_STEP_CHANGE, type = ExchangeTypes.FANOUT)
	))
	public void stepChange(StepChangeMsg stepChangeMsg) {

		if (events != null) {
			for (StepChangeEvent event : events) {
				try {
					event.stepChange(stepChangeMsg);
				} catch (Exception e) {
					logger.error("处理步数变化消息出错", e);
				}
			}
		}

	}
}
